package com.wh.springbootshiromanyrealm.realm;

import com.wh.springbootshiromanyrealm.pojo.Admin;
import com.wh.springbootshiromanyrealm.service.IAdminService;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @Auther: HuiEr
 * @Date: 2020/3/4 22:06
 * @Description
 */
public class MyAdminRealm extends AuthorizingRealm {
    @Autowired
    private IAdminService userService;

    @Override
    public String getName() {
        return "adminrealm";
    }

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        String principal = (String)authenticationToken.getPrincipal();
        Admin user = userService.findUserByName(principal);
        if (user == null){
            return null;
        }
        SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(user.getUsername(), user.getPassword(),getName());

        return simpleAuthenticationInfo;
    }

}
